home *** CD-ROM | disk | FTP | other *** search
/ Mission 3 / Mission 3.zip / Mission 3.iso / texte / qed / src / av.c < prev    next >
C/C++ Source or Header  |  1998-10-19  |  11KB  |  500 lines

  1. #include "global.h"
  2. #include "av.h"
  3. #include "comm.h"
  4. #include "dd.h"
  5. #include "options.h"
  6. #include "poslist.h"
  7. #include "rsc.h"
  8. #include "window.h"
  9.  
  10. /* exportierte Variablen *****************************************************/
  11. int    av_shell_id = -1,                /* ID des Desktops */
  12.         av_shell_status = 0;            /* Welche AV_* kann Desktop */
  13.  
  14. /* lokale Variablen **********************************************************/
  15.  
  16. /* Hilfe-System **************************************************************/
  17.  
  18. #ifndef AC_HELP
  19. #define AC_HELP        1025
  20. #endif
  21.  
  22. /*
  23.  * Generiert aus einem Pfadnamen den GEM-Namen (max. acht Zeichen, ggf.
  24.  * mit ' ' aufgefüllt) und überprüft ihn auf ST-GUIDE.
  25. */
  26. static bool make_name(char *longname, char *shortname, bool must_stg)
  27. {
  28.     int    len, i;
  29.     char    *p;
  30.  
  31.     if (longname[0] == EOS)
  32.     {
  33.         note(1, 0, NOHELP);
  34.         strcpy(shortname, "");
  35.         return FALSE;
  36.     }
  37.     else
  38.     {
  39.         file_name(longname, shortname, FALSE);
  40.         str_toupper(shortname);
  41.         p = strrchr(shortname, '.');                /* Extension abschneiden */
  42.         *p = '\0';
  43.         len = (int) strlen(shortname);
  44.         for (i = len; i < 8; i++)
  45.             strcat(shortname, " ");
  46.         if (must_stg && (strcmp(shortname, "ST-GUIDE") != 0))
  47.         {
  48.             note(1, 0, NOSTG);
  49.             return FALSE;
  50.         }
  51.         else
  52.             return TRUE;
  53.     }
  54. }
  55.  
  56.  
  57. /*
  58.  * Liefert die app_id des übergebenen Programmes zurück. Falls es noch
  59.  * nicht läuft, wird es mit arg gestartet.
  60. */
  61. static int get_id(char* name, char *path, char *arg, bool *started)
  62. {
  63.     int        ret = -1;
  64.     char        *p, help[128] = "";
  65.     bool    s = FALSE;
  66.  
  67.     if (name[0] != EOS)
  68.     {
  69.         ret = appl_find(name);
  70.         if (ret < 0)                            /* ggf. als Programm nachstarten */
  71.         {
  72.             if (arg != NULL)
  73.             {
  74.                 strcpy(help, " ");
  75.                 strcat(help, arg);
  76.                 help[0] = (char)strlen(help);
  77.             }
  78.             p = strrchr(path, '.');
  79.             if (file_exists(path) && ((strncmp(p, ".APP", 3) == 0) || (strncmp(p, ".PRG", 3) == 0) ||
  80.                                              (strncmp(p, ".app", 3) == 0) || (strncmp(p, ".prg", 3) == 0)))
  81.             {
  82.                 if (gl_gem >= 0x400)
  83.                 {
  84.                     ret = shel_write(1, 1, 1, path, help);
  85.                     s = (ret > 0);
  86.                 }
  87.                 else if (gl_magx)
  88.                 {
  89.                     ret = shel_write(1, 1, 100, path, help);
  90.                     s = (ret > 0);
  91.                 }
  92.                 else
  93.                 {
  94.                     note(1, 0, HELPSTART);
  95.                     ret = -1;
  96.                 }
  97.             }
  98.             else
  99.             {
  100.                 snote(1, 0, HELPNOTF, helpprog);
  101.                 ret = - 1;
  102.             }
  103.         }
  104.     }
  105.     if (started != NULL)
  106.         *started = s;
  107.     return ret;
  108. }
  109.  
  110. static void send_help(int id, char *str)
  111. {
  112.     if (debug_level & DBG_AV)
  113.         debug("send_help: '%s' to %d\n", str, id);
  114.     memset(msgbuff, 0, (int)sizeof(msgbuff));
  115.     msgbuff[0] = AC_HELP;
  116.     msgbuff[1] = gl_apid;
  117.     strcpy(global_str1, str);
  118.     *(char **)(msgbuff + 3) = global_str1;
  119.     send_msg(id);
  120. }
  121.  
  122. bool call_help(char *str)
  123. {
  124.     int        help_id;
  125.     FILENAME    name;
  126.  
  127.     wake_mouse();
  128.     if (make_name(helpprog, name, FALSE))
  129.     {
  130.         help_id = get_id(name, helpprog, NULL, NULL);
  131.         if (help_id > 0)
  132.         {
  133.             send_help(help_id, str);
  134.             return TRUE;
  135.         }
  136.     }
  137.     return FALSE;
  138. }
  139.  
  140. bool call_hyp(char *data)
  141. {
  142.     int        stg_id;
  143.     char         help[80];
  144.     FILENAME name;
  145.     bool        started;
  146.  
  147.     if (helpprog[0] == EOS)
  148.     {
  149.         note(1, 0, NOHELP);
  150.         return FALSE;
  151.     }
  152.     wake_mouse();
  153.     if (make_name(helpprog, name, TRUE))
  154.     {
  155.         strcpy(help, "*:\\qed.hyp ");
  156.         strcat(help, data);
  157.  
  158.         stg_id = get_id(name, helpprog, help, &started);
  159.         if (debug_level & DBG_AV)
  160.             debug("call_hyp: '%s' to %d\n", data, stg_id);
  161.         if (stg_id > 0 && !started)            /* nicht gestartet, da es bereits lief! */
  162.             send_vastart(stg_id, help);        /* -> also Message schicken */
  163.         return (stg_id > 0);
  164.     }
  165.     return FALSE;
  166. }
  167.  
  168.  
  169. static void get_my_name(char *my_name)
  170. {
  171.     char    str[12];
  172.     int    d, i, id;
  173.  
  174.     if ((appl_xgetinfo(4, &d, &d, &i, &d)) && (i == 1))    /* gibts appl_search? */
  175.     {
  176.         i = appl_search(0, str, &d, &id);
  177.         while (i != 0)
  178.         {
  179.             if (id == gl_apid)
  180.                 break;
  181.             i = appl_search( 1, str, &d, &id);
  182.         }
  183.     }
  184.     if (i != 0)
  185.     {
  186.         strcpy(my_name, str);
  187.         for (i = (int)strlen(my_name); i < 8; i++)
  188.             strcat(my_name, " ");
  189.     }
  190.     else
  191.         strcpy(my_name, "");
  192.     if    (debug_level & DBG_AV)
  193.         debug("get_my_name()= %s.\n", my_name);
  194. }
  195.  
  196. /* AV-Protokoll **************************************************************/
  197.  
  198. static char    *glob_data = NULL;
  199.  
  200. static void send_avprot(void)
  201. {
  202.     if (av_shell_id >= 0)
  203.     {
  204.         memset(msgbuff, 0, (int)sizeof(msgbuff));
  205.         msgbuff[0] = AV_PROTOKOLL;
  206.         msgbuff[1] = gl_apid;
  207.         msgbuff[3] = (2|16);        /* VA_START, Quoting */
  208.         get_my_name(global_str1);
  209.         *(char **) (msgbuff + 6) = global_str1;
  210.         send_msg(av_shell_id);
  211.         if (debug_level & DBG_AV)
  212.             debug("AV_PROTOKOLL\n");
  213.     }
  214. }
  215.  
  216. static void send_avexit(void)
  217. {
  218.     if ((av_shell_id >= 0) && (av_shell_status & 1024))
  219.     {
  220.         memset(msgbuff, 0, (int)sizeof(msgbuff));
  221.         msgbuff[0] = AV_EXIT;
  222.         msgbuff[1] = gl_apid;
  223.         msgbuff[3] = gl_apid;
  224.         send_msg(av_shell_id);
  225.         if (debug_level & DBG_AV)
  226.             debug("AV_EXIT\n");
  227.     }
  228. }
  229.  
  230. bool send_avkey(int ks, int kr)
  231. {
  232.     bool    b = FALSE;
  233.  
  234.     if ((av_shell_id >= 0) && (av_shell_status & 1))
  235.     {
  236.         wake_mouse();
  237.         memset(msgbuff, 0, (int)sizeof(msgbuff));
  238.         msgbuff[0] = AV_SENDKEY;
  239.         msgbuff[1] = gl_apid;
  240.         msgbuff[3] = ks;
  241.         msgbuff[4] = kr;
  242.         b = send_msg(av_shell_id);
  243.         if (debug_level & DBG_AV)
  244.             debug("AV_SENDKEY (%d,%d)\n", ks, kr);
  245.     }
  246.     return b;
  247. }
  248.  
  249. void send_avwinopen(int handle)
  250. {
  251.     if ((av_shell_id >= 0) && (wind_cycle))    /* Fensterwechsel global */
  252.     {
  253.         memset(msgbuff, 0, (int)sizeof(msgbuff));
  254.         msgbuff[0] = AV_ACCWINDOPEN;
  255.         msgbuff[1] = gl_apid;
  256.         msgbuff[3] = handle;
  257.         send_msg(av_shell_id);
  258.         if (debug_level & DBG_AV)
  259.             debug("AV_ACCWINDOPEN (%d)\n", handle);
  260.     }
  261. }
  262.  
  263. void send_avwinclose(int handle)
  264. {
  265.     if ((av_shell_id >= 0) && (wind_cycle))    /* Fensterwechsel global */
  266.     {
  267.         memset(msgbuff, 0, (int)sizeof(msgbuff));
  268.         msgbuff[0] = AV_ACCWINDCLOSED;
  269.         msgbuff[1] = gl_apid;
  270.         msgbuff[3] = handle;
  271.         send_msg(av_shell_id);
  272.         if (debug_level & DBG_AV)
  273.             debug("AV_ACCWINDCLOSED (%d)\n", handle);
  274.     }
  275. }
  276.  
  277. void send_avdrag(int wh, int m_x, int m_y, int kstate, int data_type)
  278. {
  279.     if ((av_shell_id >= 0) && (av_shell_status & 512))
  280.     {
  281.         int    app, d;
  282.         
  283.         wind_get(wh, WF_OWNER, &app, &d, &d, &d);
  284.         memset(msgbuff, 0, (int)sizeof(msgbuff));
  285.         msgbuff[0] = AV_DRAG_ON_WINDOW;
  286.         msgbuff[1] = gl_apid;
  287.         msgbuff[3] = m_x;
  288.         msgbuff[4] = m_y;
  289.         msgbuff[5] = kstate;
  290.  
  291.         switch (data_type)
  292.         {
  293.             case DRAGDROP_PATH :        /* ein Name */
  294.                 if (debug_level & DBG_AV)
  295.                     debug("AV_DRAG_TO_WIN single file\n");
  296.                 strcpy(global_str1, drag_filename);
  297.                 *(char **) (msgbuff + 6) = global_str1;
  298.                 break;
  299.  
  300.             case DRAGDROP_DATA :        /* mehrere Namen */
  301.                 if (debug_level & DBG_AV)
  302.                     debug("AV_DRAG_TO_WIN multible files\n");
  303.                 glob_data = malloc_global(drag_data_size + 1);
  304.                 if (glob_data != NULL)
  305.                 {
  306.                     strcpy(glob_data, drag_data);
  307.                     *(char **) (msgbuff + 6) = glob_data;
  308.                 }
  309.                 break;
  310.         }
  311.         send_msg(av_shell_id);
  312.     }
  313. }
  314.  
  315. int check_avobj(int x, int y)
  316. {
  317.     int    ret = 0;
  318.         
  319.     if (av_shell_id >= 0)
  320.     {
  321.         memset(msgbuff, 0, (int)sizeof(msgbuff));
  322.         msgbuff[0] = AV_WHAT_IZIT;
  323.         msgbuff[3] = x;
  324.         msgbuff[4] = y;
  325.         
  326.         send_msg(av_shell_id);
  327.         evnt_mesag(msgbuff);
  328.         if (msgbuff[0] == VA_THAT_IZIT)
  329.         {
  330.             ret = msgbuff[4];
  331.             if (debug_level & DBG_AV)
  332.                 debug("VA_THAT_IZIT(%d, %d): %d\n", x, y, ret);
  333.         }
  334.     }
  335.     return ret;
  336. }
  337.  
  338. static void send_avstarted(int id, int m3, int m4)
  339. {
  340.     memset(msgbuff, 0, (int)sizeof(msgbuff));
  341.     msgbuff[0] = AV_STARTED;
  342.     msgbuff[3] = m3;
  343.     msgbuff[4] = m4;
  344.     if (debug_level & DBG_AV)
  345.         debug("AV_STARTED (%d)\n", id);
  346.     send_msg(id);
  347. }
  348.  
  349. /*
  350.  * Zerlegt VA_START-Arg in einzelne Dateinamen.
  351.  * Quoting wird korrekt ausgewertet ('arg 1', 'arg''s')
  352. */
  353. static bool parse_vaarg(POSENTRY **list, char *arg)
  354. {
  355.     int        i, j, len;
  356.     PATH        filename;
  357.     bool    in_quote = FALSE;
  358.     
  359.     len = (int)strlen(arg);
  360.     if (len > 0)
  361.     {
  362.         i = 0;
  363.         j = 0;
  364.         strcpy(filename, "");
  365.         while (i < len)
  366.         {
  367.             if (arg[i] == '\'')
  368.             {
  369.                 i++;
  370.                 if (in_quote)
  371.                 {
  372.                     if (arg[i] == '\'')                    /* Doppeltes ' */
  373.                     {
  374.                         filename[j++] = '\'';
  375.                         i++;
  376.                     }
  377.                     else
  378.                         in_quote = FALSE;
  379.                 }
  380.                 else
  381.                     in_quote = TRUE;
  382.             }
  383.             if ((arg[i] != ' ') || ((arg[i] == ' ' && in_quote)))
  384.                 filename[j++] = arg[i++];
  385.             else
  386.             {
  387.                 filename[j++] = '\0';
  388.                 insert_poslist(list, filename, 0, 0);
  389.                 j = 0;
  390.                 i++; 
  391.             }
  392.         }
  393.         if (j > 0)
  394.         {
  395.             filename[j++] = '\0';
  396.             insert_poslist(list, filename, 0, 0);
  397.         }
  398.         return TRUE;
  399.     }
  400.     return FALSE;
  401. }
  402.  
  403.  
  404. void handle_av(int msgbuff[])
  405. {
  406.     char        *str_p, *arg;
  407.     int        kstate, d;
  408.     POSENTRY    *va_list = NULL;
  409.  
  410.     switch (msgbuff[0])
  411.     {
  412.         case VA_START :
  413.             if (all_iconified)
  414.                 all_uniconify(NULL, NULL);
  415.             str_p = *(char **)(msgbuff + 3);
  416.             if (str_p != NULL)
  417.             {
  418.                 arg = strdup(str_p);
  419.                 send_avstarted(msgbuff[1], msgbuff[3], msgbuff[4]);
  420.  
  421.                 if ((debug_level & DBG_AV) || (debug_level & DBG_ARG))
  422.                     debug("VA_START %s\n", arg);
  423.  
  424.                 if (strlen(arg) > 0 && parse_vaarg(&va_list, arg))
  425.                 {
  426.                     if (debug_level & DBG_AV)
  427.                         debug("va_arg= %s\n", str_p);
  428.                     open_poslist(va_list);
  429.                     delete_poslist(&va_list);
  430.                 }
  431.                 free(arg);
  432.             }
  433.             break;
  434.  
  435.         case VA_PROTOSTATUS :
  436.             if (debug_level & DBG_AV)
  437.                 debug("VA_PROTSTATUS %u\n", (unsigned short)msgbuff[3]);
  438.             av_shell_status = msgbuff[3];
  439.             if (wind_cycle && !(av_shell_status & 64))
  440.                 wind_cycle = FALSE;            /* glob. Fensterwechsel abschalten */
  441.             break;
  442.  
  443.         case VA_DRAG_COMPLETE :
  444.             if (debug_level & DBG_AV)
  445.                 debug("VA_DRAG_COMPLETE.\n");
  446.             if (glob_data != NULL)
  447.             {
  448.                 free(glob_data);
  449.                 glob_data = NULL;
  450.             }
  451.             break;
  452.  
  453.         case VA_DRAGACCWIND :                /* bei D&D mit glob. Fensterwechsel */
  454.             str_p = *(char **)(msgbuff + 6);
  455.             if (str_p != NULL)
  456.             {
  457.                 graf_mkstate(&d, &d, &d, &kstate);
  458.                 handle_avdd(msgbuff[3], kstate, str_p);
  459.             }
  460.             break;
  461.  
  462.         case AV_SENDKEY :
  463.             if (debug_level & DBG_AV)
  464.                 debug("AV_SENDKEY von %d: %d, %d\n", msgbuff[1], msgbuff[3], msgbuff[4]);
  465.             if ((msgbuff[3] == 4) && (msgbuff[4] == 0x1117))    /* ^W */
  466.                 cycle_window();
  467.             break;
  468.             
  469.         default:
  470.             break;
  471.     }
  472. }
  473.  
  474. void init_av(void)
  475. {
  476.     int    i;
  477.     char    name[9], *p;
  478.  
  479.     p = getenv("AVSERVER");
  480.     if (p != NULL)
  481.     {
  482.         strncpy(name, p, 8);
  483.         name[8] = EOS;
  484.         for (i = (int)strlen(name); i < 8; i++)
  485.             strcat(name, " ");
  486.         i = appl_find(name);
  487.         if (i >= 0)
  488.             av_shell_id = i;
  489.     }
  490.     if (debug_level & DBG_AV)
  491.         debug("av_shell_id= %d\n", av_shell_id);
  492.  
  493.     send_avprot();
  494. }
  495.  
  496. void term_av(void)
  497. {
  498.     send_avexit();
  499. }
  500.